#include "admin_function.h"
#include"share_data.h"
#include"Upload_database.h"
#include"list.h"
#include"sys_log.h"
#include"heart_beat.h"
// 管理员操作
int admin_function(int admin_judge,int listenfd)
{
	static int ret = 0;
	char dest[MAX_BUFFER] = { 0 };
	if (admin_judge == 1) {


		printf("管理员进入水泵和具体数据操作界面成功！！！\n");
		LOG_I("管理员进入水泵和具体数据操作界面成功\n");
		while(1)
		{
			memset(dest, 0, sizeof(dest));//这个是为了防止上次发送的数据没有清空，导致接收的数据有残余
			printf("正在等待客户端发送-----\n");
			ret = recv(listenfd, dest, MAX_BUFFER, 0);
			if (ret == -1) {
				printf("管理员函数界面recv error\n");
				perror("recv");
			}
			if (ret == 0) {
				printf("客户端已经关闭了连接\n");
				break;
			}
			if (ret > 0) {
				handle_client_message(listenfd, dest); // 正常处理消息并更新心跳时间
			}
			printf("本次接收的字节大小是：%d\n",ret);
			printf("刚收到并未解析的数据：%s\n", dest);
			LOG_D("管理员界面解析到的数据：%s\n", dest);
			// 解析json
			cJSON* json = NULL;
			json = cJSON_Parse(dest);
			cJSON* node = NULL;
			node = cJSON_GetObjectItem(json, "type");
			if (node == NULL) {
				printf("admin.c1，解析大type错误\n");
				exit(-1);
			}

			if (node->type == cJSON_Number) {//判断是水泵还是具体数据

				if (node->valueint == 3) {//如果是水泵
					node = cJSON_GetObjectItem(json, "pump_type");
					if (node == NULL) {
						printf("admin.c2水泵小type是空的\n");
						LOG_E("admin.c2水泵小type是空的\n");
						exit(-1);
					}
					if (node->type == cJSON_Number) {
						if (node->valueint == 1) {//如果水泵要写
							WP pump;
							memset(&pump, 0, sizeof(WP));

							//解析水泵数据到WP本地变量pump中
							pump = unpack_pump(dest);

							//把数据存进数据库中
							int flag1 = 0;
							MYSQL* pump_connect = NULL;
							pump_connect = connect_mysql();
							flag1 = pump_data_insert_database3(&pump, pump_connect);


							//回发客户端
							STA count;
							if (flag1 == 1) {
								count.type = 1;
								strcpy(count.state, "水泵数据写入成功");
								LOG_I("水泵数据写入成功\n");
								Postback_message(listenfd, &count);
								mysql_close(pump_connect);
							}
							else {
								count.type = 0;
								strcpy(count.state, "水泵数据写入失败");
								LOG_I("水泵数据写入失败\n");
								Postback_message(listenfd, &count);
								mysql_close(pump_connect);
							}


						}
						else if (node->valueint == 2) {//如果水泵要删除
							WP pump;
							memset(&pump, 0, sizeof(WP));
							//解析水泵数据到WP本地变量pump中
							pump = unpack_pump(dest);

							//把数据从数据库中删除
							int flag2 = 0;
							MYSQL* pump_connect = NULL;
							pump_connect = connect_mysql();
							flag2 = pump_data_delete_database3(&pump, pump_connect);


							//回发客户端
							STA count;
							if (flag2 == 1) {
								count.type = 1;
								strcpy(count.state, "水泵数据删除成功");
								LOG_I("水泵数据删除成功\n");
								Postback_message(listenfd, &count);
								mysql_close(pump_connect);
							}
							else {
								count.type = 0;
								strcpy(count.state, "水泵数据删除失败");
								LOG_I("水泵数据删除失败\n");
								Postback_message(listenfd, &count);
								mysql_close(pump_connect);
							}

						}
						else if (node->valueint == 3) {//如果水泵要查

							//把水泵数据从数据库中查出来
							pump_list(listenfd);
						}
					}

				}

				else if (node->valueint == 4) {//如果是具体数据
						node = cJSON_GetObjectItem(json, "data_type");
						if (node == NULL) {
							printf("具体数据的小type是空\n");
							exit(-1);
						}
						if (node->type == cJSON_Number) 
						{
							if (node->valueint == 1) {//具体数据下的1号功能------------查询最新
								int pump_id = 0;
								node = cJSON_GetObjectItem(json, "pump_id");
								if (node == NULL) {
									printf("客户端要查看的pump_id是空\n");
									exit(-1);
								}
								if (node->type == cJSON_Number) {
									pump_id = node->valueint;//获取客户端具体要查看的水泵号
								}
								printf("客户端要查看的pump_id是%d\n", pump_id);
								//把水泵号给到函数
								new_data_list(listenfd,pump_id);
								LOG_I("查询最新成功\n");
							}
							if (node->valueint == 2)//具体数据下的2号功能------------查询全部
							{
								int pump_id = 0;
								node = cJSON_GetObjectItem(json, "pump_id");
								if (node == NULL) {
									printf("客户端要查看的pump_id是空\n");
									exit(-1);
								}
								if (node->type == cJSON_Number) {
									pump_id = node->valueint;//获取客户端具体要查看的水泵号
								}
								printf("客户端要查看的pump_id是%d\n", pump_id);
								all_data_list(listenfd, pump_id);
								LOG_I("查询全部成功\n");
							}
						}

				}
			}
		}

	}
	else {
		printf("管理员进入水泵和具体数据操作界面失败！！！\n");
	}
	return 0;
}

//解析水泵数据到WP结构体变量中
WP unpack_pump(char* dest) {
	WP pump;
	memset(&pump, 0, sizeof(WP));
	cJSON* json = NULL;
	json = cJSON_Parse(dest);
	cJSON* node = NULL;
	node = cJSON_GetObjectItem(json, "id");
	if (node == NULL) {
		printf("admin.c3can't find user_type\n");
		exit(-1);
	}
	if (node->type == cJSON_Number) {
		pump.id = node->valueint;
	}

	node = cJSON_GetObjectItem(json, "pump_name");
	if (node == NULL) {
		printf("admin.ccan't find user_name\n");
		exit(-1);
	}
	if (node->type == cJSON_String) {
		strcpy(pump.pump_name, node->valuestring);
	}
	cJSON_Delete(json);
	return pump;
}

//从数据库读出的水泵数据进入到链表中再回给客户端------------供水泵查询使用
void pump_list(int listenfd) {
	int flag = 0;
	MYSQL* pump_connect = NULL;
	pump_connect = connect_mysql();

	WP* head = NULL;
	head = List_init_WP();
	head = read_mysql_to_list3(pump_connect,head);	//读取完应该拿head接收链表
	printf("这是读取的水泵链表：\n");
	Printf_list_WP(head);
	int ret = 0;
	WP* p = head->Next;
	WP temp;
	int number = 0;
	while (p != NULL) {
		memset(&temp, 0, sizeof(WP));
		temp.id = p->id;
		strcpy(temp.pump_name, p->pump_name);
		ret = send(listenfd, &temp,sizeof(temp), 0);
		if (ret == -1) {
			printf("send list error\n");
			perror("send");
		}
		sleep(0.5);
		printf("第%d次发送--------id:%d,水泵名字:%s\n", number, temp.id, temp.pump_name);
		p = p->Next;
		number++;
	}
	freeLink_WP(head);
	WP temp1;
	temp1.id = 0;
	ret = send(listenfd, &temp1, sizeof(temp1), 0);
	if (ret == -1) {
		printf("send list error\n");
		perror("send");
	}
	printf("第%d次发送--------id:%d,水泵名字:空\n", number, temp1.id);
	mysql_close(pump_connect);

}





//从数据库读出的具体数据进入到链表中再回给客户端------------供具体数据的查询最新使用
void new_data_list(int listenfd,int pump_id) {
	int flag = 0;
	MYSQL* data_connect = NULL;
	data_connect = connect_mysql();

	DAT* head = NULL;
	head = List_init_DAT();
	head = read_mysql_to_data_list(data_connect,head);	//读取完应该拿head接收链表
	printf("这是读取的具体数据链表：\n");
	Printf_list_DAT(head);
	int ret = 0;
	DAT* p = head->Next;      // 从链表的第一个有效节点开始遍历
	DAT temp, last_match;     // temp用于临时存储，last_match用于存储最后匹配的数据
	int number = 0;
	int match_found = 0;      // 用于判断是否找到匹配的数据

	while (p != NULL) {
		memset(&temp, 0, sizeof(DAT));     // 清空temp结构体的数据
		temp.temperature = p->temperature; // 复制节点数据到temp
		temp.flow_rate = p->flow_rate;
		temp.liquid_level = p->liquid_level;
		temp.valve_state = p->valve_state;
		temp.negative_pressure = p->negative_pressure;
		temp.pressure = p->pressure;
		temp.pump_id = p->pump_id;
		strcpy(temp.time, p->time);

		if (temp.pump_id == pump_id) {     // 如果找到了匹配的pump_id
			last_match = temp;             // 记录当前匹配的temp
			match_found = 1;               // 标记找到了匹配
		}
		p = p->Next;   // 移动到下一个节点
		number++;
	}

	if (match_found) {   // 如果找到了匹配的数据
		char* dest = NULL;
		dest = data_pack(&last_match);
		ret = send(listenfd,dest,1024, 0);
		printf("已发送当前水泵的最新数据:%s", dest);
		if (ret == -1) {
			printf("send list error\n");
			perror("send");
		}
	}
	else {
		printf("没有找到匹配的pump_id\n");
	}
	freeLink_DAT(head);

	mysql_close(data_connect);
}



//从数据库读出的具体数据进入到链表中再回给客户端------------供具体数据的查询所有使用
void all_data_list(int listenfd, int pump_id) {
	int flag = 0;
	MYSQL* data_connect = NULL;
	data_connect = connect_mysql();

	DAT* head = NULL;
	head = List_init_DAT();
	head = read_mysql_to_data_list(data_connect, head);	//读取完应该拿head接收链表
	printf("这是读取的具体数据链表：\n");
	Printf_list_DAT(head);
	int ret = 0;
	DAT* p = head->Next;
	DAT temp;
	int number = 0;
	while (p != NULL) {
		memset(&temp, 0, sizeof(WP));
		temp.temperature = p->temperature;
		temp.flow_rate = p->flow_rate;
		temp.liquid_level = p->liquid_level;
		temp.valve_state = p->valve_state;
		temp.negative_pressure = p->negative_pressure;
		temp.pressure = p->pressure;
		temp.pump_id = p->pump_id;
		strcpy(temp.time, p->time);
		if (temp.pump_id == pump_id)
		{
			char* dest = NULL;
			dest = data_pack(&temp);
			ret = send(listenfd, dest, MAX_BUFFER, 0);
			printf("第 %d 次发送的是:%s",number, dest);
			if (ret == -1) {
				printf("send list error\n");
				perror("send");
			}
			sleep(1);
		}
		p = p->Next;
		number++;
	}

	freeLink_DAT(head);

	DAT end_temp;
	end_temp.pump_id = 0;
	char* dest = NULL;
	dest = data_pack(&end_temp);
	ret = send(listenfd, dest, MAX_BUFFER, 0);
	if (ret == -1) {
		printf("结束标志发送失败\n");
		perror("send");
	}
	printf("已发送结束标志,内容是:%s\n", dest);

	mysql_close(data_connect);
}


//把具体数据打包成JSON
char* data_pack(DAT* dat) {
	char* json_str = NULL;
	cJSON* json = NULL;
	json = cJSON_CreateObject();
	cJSON_AddNumberToObject(json, "temperature", dat->temperature);
	cJSON_AddNumberToObject(json, "flow_rate", dat->flow_rate);
	cJSON_AddNumberToObject(json, "liquid_level", dat->liquid_level);
	cJSON_AddNumberToObject(json, "valve_state", dat->valve_state);
	cJSON_AddNumberToObject(json, "negative_pressure", dat->negative_pressure);
	cJSON_AddNumberToObject(json, "pressure", dat->pressure);
	cJSON_AddStringToObject(json, "time", dat->time);
	cJSON_AddNumberToObject(json, "pump_id", dat->pump_id);
	char* dest = cJSON_Print(json);
	return dest;
}







